package com.sjm.suchbear.bluetoothtest;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Suchbear on 2019/4/8.
 */
public class BlueToothListAdapter extends BaseAdapter {
    private Context context;
    private List<BluetoothDevice> connectedBlueTooths;
    private ViewHolder viewHolder;

    public BlueToothListAdapter(Context context, List<BluetoothDevice> connectedBlueTooths) {
        this.context = context;
        this.connectedBlueTooths = connectedBlueTooths;
    }

    @Override
    public int getCount() {
        if (connectedBlueTooths != null) {
            return connectedBlueTooths.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return connectedBlueTooths.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.simpleitem, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvName.setText(connectedBlueTooths.get(i).getName());
        viewHolder.tvAddress.setText(connectedBlueTooths.get(i).getAddress());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_address)
        TextView tvAddress;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
